package com.kingsoft.dc.khaos.optimize

import com.kingsoft.dc.khaos.metadata.Dependency
import com.kingsoft.dc.khaos.util.Logging
import org.json4s.jackson.JsonMethods.parse

import scala.collection.immutable.List
import scala.collection.mutable

/**
  * Created by chengguangqing on 2019/7/31.
  */
object DependOptimize extends Logging{

  def formatSourceSeq(dependencys:List[Dependency]): Unit={
    if(dependencys.size > 1)
      {
        log.info("重新排序入度")
        log.info("原始Dependency:"+dependencys)
        val currentIndex = mutable.ListBuffer[Int]()
        dependencys.foreach{ dep =>
          currentIndex+=Integer.valueOf(dep.sourceSeq)
        }

        var newIndex = 0
        currentIndex.sorted.foreach{ i =>
          dependencys.filter(_.sourceSeq.equals(i.toString)).head.setSourceSeq(String.valueOf(newIndex))
          newIndex +=1
        }
        log.info("重排后Dependency:"+dependencys)
      }
  }

  def appendSoruceTableName(strategy:String,dependencys:List[Dependency]): Unit ={
    val parser = parse(strategy, true)
    val dbName = (parser \ "strategy" \ "config" \ "db_name").toString
    val tableName= (parser \ "strategy" \ "config" \ "table_name").toString

    dependencys.foreach{ dependency =>
      dependency.setSourceTable(dbName.trim+"."+tableName.trim)
    }
  }

  def main(args: Array[String]): Unit = {
   /*val deps = List(new Dependency("A","2","B","0")
     ,new Dependency("A","6","B","0")
     ,new Dependency("A","100","B","0"))

    deps.foreach{ dep =>
      println(dep.sourceSeq)
    }

    formatSourceSeq(deps)

    deps.foreach{ dep =>
      println(dep.sourceSeq)
    }*/

    val start = System.nanoTime()

    val start2 = System.currentTimeMillis();
    println("1>"+start + "2>"+start2)

    Thread.sleep(2000)

    print((System.nanoTime()-start2)/1e9)
  }
}
